<?php
    $sql = "SELECT img_path,file_path FROM promotion";
    $result = $db->Execute($sql);
    $result->Close();
?>


<script type="text/javascript">
    $(function() {
        $("#tabs").tabs();
    });
    
    $(document).ready(function(){
        
        $('.submitFileBt').click(function(){
            $("#file_success_msg").css('display','none');
            $("#file_error_msg").css('display','none');
            $('.submit_file_indicator').css('display','inline');
            $('#promoFileFrm').attr('method','post');
            $('#promoFileFrm').attr('target','ajaxIfrm');
            $('#promoFileFrm').submit();
        });
        
        $('.submitImgBt').click(function(){
            $("#img_success_msg").css('display','none');
            $("#img_error_msg").css('display','none');
            $('.submit_img_indicator').css('display','inline');
            $('#promoImgFrm').attr('method','post');
            $('#promoImgFrm').attr('target','ajaxIfrm');
            $('#promoImgFrm').submit();
        });
        
        $('#deleteFile').click(function(){
            if(confirm('Delete file?')){
                $('.submit_file_indicator').css('display','inline');
                var data = 'action=delete_file&oldfilepath='+$("#oldfilepath").val();
                //alert(data);
                $.post('function/homePromotion.php',data,function(data){
                    $('.submit_file_indicator').css('display','none');
                    if(trim(data)=='success')
                    {
                        $("#file_success_msg").html('ลบไฟล์เรียบร้อยแล้ว');
                        $("#file_success_msg").show('highlight',3000,function(){
                            $("#file_success_msg").hide('highlight',3000);
                            setInterval("window.location='index.php?page=home&part=promotion';",3000);
                        });
                    }
                    else
                    {
                        $("#file_error_msg").html('ไม่สามารถลบไฟล์ได้ กรุณาลองใหม่อีกครั้ง');
                        $("#file_error_msg").show('highlight',3000,function(){
                            $("#file_error_msg").hide('highlight',3000);
                        });
                    }
                });
            }
        });
    });
    
</script>
<h3 class="pageTitle">จัดการโปรโมชั่น</h3>
<div id="tabs" class="carAdd">
    
    <ul>
        <li id="car_"><a href="#tabs-1" title="Add files">Image</a></li>
        <li id="car_"><a href="#tabs-2" title="Add files">File</a></li>
    </ul>
    
    <div id="tabs-1" class="promoImgFrm">
        <form name="promoImgFrm" id="promoImgFrm" action='function/homePromotion.php' enctype='multipart/form-data'>
            <img src="<?php echo $iniVars['Locations']['siteurl'],$result->fields['img_path']; ?>" />
            <p style="margin-bottom:10px;"><input name="promoImg" type="file" class="input medium" id="promoImg" /></p>
            <input type="hidden" name="oldpath" value="<?php echo $result->fields['img_path']; ?>" />
            <input type="hidden" name="action" value="upload_img" />
            <input type="button" class="button submitImgBt" value="Save" />
            &nbsp;<img src="images/ajax-loader.gif" class="ajax_load_img submit_img_indicator"/>
        </form>
        <div class="information canhide png_bg">รูปภาพขนาดความกว้างแนะนำ 560 pixel</div>
        <div class="fail canhide png_bg ajax_msg" id="img_error_msg"></div>
        <div class="success canhide ajax_msg" id="img_success_msg"><p></p></div>
    </div>
    
    <div id="tabs-2" class="promoFileFrm">
        <form name="promoFileFrm" id="promoFileFrm" action='function/homePromotion.php' enctype='multipart/form-data'>
            <?php
                if($result->fields['file_path']!='' AND file_exists($iniVars['Locations']['sitepath'].$result->fields['file_path'])){
            ?>
            <a href="<?php echo $iniVars['Locations']['siteurl'],$result->fields['file_path']; ?>"><?php echo $iniVars['Locations']['siteurl'],$result->fields['file_path']; ?></a> <img src="images/delete.png" id="deleteFile" class="img_link" alt="Delete File" />
            <p style="margin-bottom:10px;"><input name="promoFile" type="file" class="input medium" id="promoFile" /></p>
            <input type="hidden" name="oldfilepath" id="oldfilepath" value="<?php echo $result->fields['file_path']; ?>" />
            <input type="hidden" name="action" value="upload_file" />
            <?php
                }else{
            ?>
            <p style="margin-bottom:10px;"><input name="promoFile" type="file" class="input medium" id="promoFile" /></p>
            <input type="hidden" name="oldfilepath" id="oldfilepath" value="" />
            <input type="hidden" name="action" value="upload_file" />
            <?php
                }
            ?>
            <input type="button" class="button submitFileBt" value="Save" />
            &nbsp;<img src="images/ajax-loader.gif" class="ajax_load_img submit_file_indicator"/>
        </form>
        <div class="information canhide png_bg">ไฟล์นามสกุล PDF ขนาดไม่เกิน 2 MB เท่านั้น</div>
        <div class="fail canhide png_bg ajax_msg" id="file_error_msg"></div>
        <div class="success canhide ajax_msg" id="file_success_msg"><p></p></div>
    </div>
    
    <iframe name="ajaxIfrm" class="ajaxIfrm" ></iframe>
</div>